package com.ikingtech.platform.service.system.user.service.repository;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ikingtech.framework.sdk.context.security.Me;
import com.ikingtech.framework.sdk.utils.Tools;
import com.ikingtech.platform.service.system.user.entity.UserRoleDO;
import com.ikingtech.platform.service.system.user.mapper.UserRoleMapper;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author tie yan
 */
@Service
public class UserRoleRepository extends ServiceImpl<UserRoleMapper, UserRoleDO> {

    public void removeByUserId(String userId, String tenantCode) {
        this.remove(Wrappers.<UserRoleDO>query().lambda()
                .eq(UserRoleDO::getUserId, userId)
                .eq(Tools.Str.isNotBlank(tenantCode), UserRoleDO::getTenantCode, tenantCode));
    }

    public void removeByUserId(String userId, String tenantCode, String appCode) {
        this.remove(Wrappers.<UserRoleDO>query().lambda()
                .eq(UserRoleDO::getUserId, userId)
                .eq(Tools.Str.isNotBlank(tenantCode), UserRoleDO::getTenantCode, tenantCode)
                .eq(Tools.Str.isNotBlank(appCode), UserRoleDO::getAppCode, appCode));
    }

    public void removeByRoleId(String roleId, String tenantCode) {
        this.remove(Wrappers.<UserRoleDO>query().lambda().eq(UserRoleDO::getRoleId, roleId).eq(UserRoleDO::getTenantCode, tenantCode));
    }

    public List<String> listUserIdByRoleIds(List<String> roleIds) {
        return this.listObjs(Wrappers.<UserRoleDO>lambdaQuery()
                .select(UserRoleDO::getUserId)
                .in(UserRoleDO::getRoleId, roleIds), String.class::cast);
    }

    public List<String> listUserIdByRoleId(String roleId) {
        return this.listObjs(Wrappers.<UserRoleDO>lambdaQuery()
                .select(UserRoleDO::getUserId)
                .eq(UserRoleDO::getRoleId, roleId)
                .eq(UserRoleDO::getTenantCode, Me.tenantCode()), String.class::cast);
    }

    public List<UserRoleDO> listByDomainCodeAndTenantCodeAndAppCodeAndUserIds(String domainCode, String tenantCode, String appCode, List<String> userIds) {
        if (Tools.Coll.isBlank(userIds)) {
            return new ArrayList<>();
        }
        return this.list(Wrappers.<UserRoleDO>lambdaQuery()
                .in(UserRoleDO::getUserId, userIds)
                .eq(Tools.Str.isNotBlank(domainCode), UserRoleDO::getDomainCode, domainCode)
                .eq(Tools.Str.isNotBlank(tenantCode), UserRoleDO::getTenantCode, tenantCode)
                .eq(Tools.Str.isNotBlank(appCode), UserRoleDO::getAppCode, appCode));
    }

    public List<UserRoleDO> listByUserId(String userId) {
        return this.list(Wrappers.<UserRoleDO>lambdaQuery().eq(UserRoleDO::getUserId, userId).eq(UserRoleDO::getTenantCode, Me.tenantCode()));
    }

    public void removeByTenantCode(String tenantCode) {
        this.remove(Wrappers.<UserRoleDO>lambdaQuery().eq(UserRoleDO::getTenantCode, tenantCode));
    }
}
